﻿using System.Diagnostics;
using Microsoft.Practices.CompositeUI.BuilderStrategies;
using Microsoft.Practices.ObjectBuilder;

namespace Microsoft.Practices.CompositeUI
{
    /// <summary>
    /// 	Defines the <see cref = "CabApplication{TWorkItem}" /> as an application having a well known lifecycle
    /// 	and a root <see cref = "WorkItem" /> instance providing the scope for the application.
    /// </summary>
    /// <typeparam name = "TWorkItem">The type of the root application work item.</typeparam>
    public abstract class CabApplication<TWorkItem> where TWorkItem : WorkItem, new()
    {
        #region Fields & Properties

        private TWorkItem rootWorkItem;

        /// <summary>
        /// 	Returns the root <see cref = "WorkItem" /> for the application.
        /// </summary>
        public TWorkItem RootWorkItem
        {
            get
            {
                return rootWorkItem;
            }
        }

        #endregion

        #region Run

        /// <summary>
        /// 	Starts the application.
        /// </summary>
        public void Run()
        {
            var builder = CreateBuilder();
        }

        #endregion

        #region Create Builder

        private Builder CreateBuilder()
        {
            var builder = new Builder();

            builder.Strategies.AddNew<EventBrokerStrategy>(BuilderStage.Initialization);
            builder.Strategies.AddNew<CommandStrategy>(BuilderStage.Initialization);
            //builder.Strategies.Add(new RootWorkItemInitializationStrategy(this.OnRootWorkItemInitialized), BuilderStage.Initialization);
            builder.Strategies.AddNew<ObjectBuiltNotificationStrategy>(BuilderStage.PostInitialization);

            builder.Policies.SetDefault<ISingletonPolicy>(new SingletonPolicy(true));
            builder.Policies.SetDefault<IBuilderTracePolicy>(new BuilderTraceSourcePolicy(new TraceSource("Microsoft.Practices.ObjectBuilder")));
            builder.Policies.SetDefault(new ObjectBuiltNotificationPolicy());

            return builder;
        }

        #endregion
    }
}